package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.exception.ServiceException;
import org.summer.lawyer.constant.enums.PayTransferStatus;
import org.summer.lawyer.domain.vo.payorder.PayOrderVo;
import org.summer.lawyer.domain.vo.payrefund.PayRefundVo;
import org.summer.lawyer.domain.vo.paytransfer.PayTransferVo;
import org.summer.lawyer.pay.client.PayClient;
import org.summer.lawyer.service.*;
import org.summer.lawyer.util.IdWorkerUtil;

import java.util.Map;

/**
 *
 */

@Tag(name = "后台相关接口", description = "支付回调通知")
@Slf4j
@SaIgnore
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/notify")
public class PayNotifyController {

    private final IPayOrderService payOrderService;

    private final IPayAppService payAppService;

    private final IPayChannelService payChannelService;

    private final ISpuOrderService spuOrderService;

    private final IPayRefundService payRefundService;

    private final IPayTransferService payTransferService;

    @Operation(summary = "支付回调")
    @PostMapping("/order/{channelId}")
    public String notifyOrder(@PathVariable("channelId") Long channelId, @RequestParam(required = false) Map<String, String> params, @RequestBody(required = false) String body) {
        log.info("[notifyOrder][channelId({})] 回调数据({}/{})", channelId, params, body);
        //1. 校验支付通道是否存在
        PayClient payClient = payChannelService.getPayClient(channelId);
        if (ObjectUtil.isNull(payClient)) {
            log.error("[notifyOrder][通道号({})] 找不到对应的支付通道", channelId);
            throw new ServiceException("找不到支付通道");
        }
        //2 解析通知数据
        PayOrderVo notify = payClient.parseOrderNotify(params, body);
        //处理回调
        payOrderService.payOrderNotify(notify);
        return "success";
    }

    @PostMapping("/refund/{channelId}")
    public String notifyRefund(@PathVariable("channelId") Long channelId,
                               @RequestParam(required = false) Map<String, String> params,
                               @RequestBody(required = false) String body) {
        log.info("[notifyRefund][channelId({})] 回调数据({}/{})]", channelId, params, body);
        //1. 校验支付通道是否存在
        PayClient payClient = payChannelService.getPayClient(channelId);
        if (ObjectUtil.isNull(payClient)) {
            log.error("[notifyRefund][channelId({})] 找不到对应的支付通道", channelId);
            throw new ServiceException("找不到支付通道");
        }
        //解析通知数据
        PayRefundVo notify = payClient.parseRefundNotify(params, body);
        //处理回调
        payRefundService.payRefundNotify(notify);
        ;
        return "success";
    }

    @PostMapping("/transfer/{channelId}")
    public String notifyTransfer(@PathVariable("channelId") Long channelId,
                                 @RequestParam(required = false) Map<String, String> params,
                                 @RequestBody(required = false) String body) {
        log.info("[notifyTransfer][channelId({})] 回调数据({}/{})]", channelId, params, body);
        //1. 校验支付通道是否存在
        PayClient payClient = payChannelService.getPayClient(channelId);
        if (ObjectUtil.isNull(payClient)) {
            log.error("[notifyTransfer][channelId({})] 找不到对应的支付通道", channelId);
            throw new ServiceException("找不到支付通道");
        }
        //解析通知数据
        PayTransferVo notify = payClient.parseTransferNotify(params, body);
        //处理回调
        payTransferService.payTransferNotify(notify);
        return "success";
    }

    /**
     * 支付回调测试
     */
    @PostMapping("/testPay")
    public String testPay(@RequestParam(required = false) Map<String, String> params) {
        String payNo = params.get("payNO");
        PayOrderVo vo = new PayOrderVo();
        vo.setPayNo(payNo);
        vo.setTradeNo(IdWorkerUtil.nextIdStr());
        vo.setPayTime(DateUtil.date());
        vo.setSuccessTime(DateUtil.toLocalDateTime(DateUtil.date()));
        payOrderService.payOrderNotify(vo);
        return "success";
    }

    /**
     * 转账回调测试
     */
    @PostMapping("/testTransfer")
    public String testTransfer(@RequestParam(required = false) Map<String, String> params) {
        String transferBatchNo = params.get("transferBatchNo");
        PayTransferVo vo = new PayTransferVo();
        vo.setTransferBatchNo(transferBatchNo);
       // vo.setTradeNo(IdWorkerUtil.nextIdStr());
        vo.setTransferStatus(PayTransferStatus.SUCCESS.getCode());
        vo.setSuccessTime(DateUtil.toLocalDateTime(DateUtil.date()));
        payTransferService.payTransferNotify(vo);
        return "success";
    }

}
